/**
 * 
 */
package com.caogt.process.action;

import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.caogt.osm.bean.Org;
import com.caogt.osm.service.IOrgService;

/**
 * @author caogt email:caogangtao@gmail.com
 * @date 2013-10-14 上午11:35:02 
 * @version V1.0  
 *
 */
@Controller
public class ManagerAnnotation {
	
	@Resource(name="orgService")
	private IOrgService orgService;
	
	@RequestMapping("/manager")
	public String manager(HttpServletRequest request){
		request.setAttribute("method", "manager");
		return "manager";
	}
	@RequestMapping("/init")
	public String init(HttpServletRequest request){
		request.setAttribute("method", "init");
		return "tree";
	}
	@RequestMapping(value="/initTree",produces="text/plain;charset=utf-8")
	@ResponseBody
	public  String initTree(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String id = request.getParameter("id");
		Org o = new Org();
		o.setParentOrgId(id);
		List<Org> orgList = orgService.selectOrgByCondition(o);
		return list2jsonForTree(orgList);
	}
	/**
	 * 添加组织
	 * @param request
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping("/addOrg")
	@ResponseBody
	public String addOrg(HttpServletRequest request) throws SQLException{
		String parentId = request.getParameter("parentId");
		String orgName = request.getParameter("orgName");
		Org org = new Org();
		org.setOrgName(orgName);
		org.setParentOrgId(parentId);
		int rest = orgService.insertOrg(org);
		if(rest > 0){
			return "{'flag':'success','msg':'添加成功！'}";
		}
		return "{'flag':'fail','msg':'添加失败！'}";
	}
	/**
	 * 删除组织
	 * @param request
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping("/deleteOrg")
	@ResponseBody
	public String deleteOrg(HttpServletRequest request) throws SQLException{
		String orgId = request.getParameter("orgId");
		int rest = orgService.deleteOrg(orgId);
		if(rest > 0){
			return "{'flag':'success','msg':'删除成功！'}";
		}
		return "{'flag':'fail','msg':'删除失败！'}";
	}
	/**
	 * 获取子组织
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/getChildrenOrg",produces="text/plain;charset=utf-8")
	@ResponseBody
	public String getChildrenOrg(HttpServletRequest request) throws Exception{
		String orgId = request.getParameter("orgId");
		Org org = new Org();
		org.setParentOrgId(orgId);
		List<Org> children = orgService.selectOrgByCondition(org);
		System.out.println(children);
		return list2jsonForTree(children);
	}
	/**
	 * 将组织信息转换成前台组织树可用格式
	 * @param orgList
	 * @return
	 * @throws Exception
	 */
	private String list2jsonForTree(List<Org> orgList) throws Exception{
		if(orgList!=null){
			int length = orgList.size();
			for(int i=0;i<length;i++){
				Org or = orgList.get(i);
				Org org = new Org();
				org.setParentOrgId(or.getOrgId());
				List<Org> list = orgService.selectOrgByCondition(org);
				if(list != null && !list.isEmpty()){
					orgList.get(i).setHasChildren(false);
				}else{
					orgList.get(i).setHasChildren(true);
				}
			}
			String temp = JSONArray.fromObject(orgList).toString();
			temp = temp.replaceAll("orgId", "id").replaceAll("orgName", "text").replaceAll("parentOrgId", "parentId").replaceAll("hasChildren", "leaf");
			return temp;
		}
		return null;
	}
}
